package cn.itsource.pethome.pet.service.impl;

import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.domain.PetDetail;
import cn.itsource.pethome.pet.mapper.PetDetailMapper;
import cn.itsource.pethome.pet.mapper.PetMapper;
import cn.itsource.pethome.pet.service.IPetService;
import cn.itsource.pethome.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    /**
     * 新增，先将数据添加到pet表，再添加到pet_detail表
     * @param pet
     */
    @Override
    @Transactional
    public void save(Pet pet) {
        super.save(pet);
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.saveByPetId(petDetail);
    }

    /**
     * 编辑，先修改pet表，再修改pet_detail表
     * @param pet
     */
    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.update(petDetail);
    }

    /**
     *  批量上架，修改上架状态的同时，修改上架时间onsaletime
     * @param ids
     * @return
     */
    @Override
    public AjaxResult batchOnSale(Long[] ids) {
        try {
            petMapper.batchOnSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络异常，请稍后再试");
        }
    }

    /**
     * 批量下架，修改上架状态的同时，修改下架时间offsaletime
     * @param ids
     * @return
     */
    @Override
    public AjaxResult batchOffSale(Long[] ids) {
        try {
            petMapper.batchOffSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络异常，请稍后再试");
        }
    }
}
